package com.future.mayi.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.adapter.RedPacketAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.RedPackResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

public class RedPacketFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.iv_null)
    ImageView ivNull;

    private int page = 0;

    /**
     * type
     * 0未使用
     * 1已使用
     * 10已过期
     */
    private int type;

    private RedPacketAdapter adapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_red_packet;
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new RedPacketAdapter(getActivity(), recyclerView, type);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        recyclerView.refresh();
    }


    @Override
    public void onRefresh() {
        page = 0;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }

    @Override
    public void OnItem(View view, int position) {


    }

    @Override
    public void OnLongItem(View view, int position) {

    }


    @SuppressWarnings("unchecked")
    private void getData() {
        new HttpRequest<RedPackResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.getRedpackList)
                .addParam("status", type)
                .addParam("p", page)
                .addParam("psize", 10)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<RedPackResponse>() {
                    @Override
                    public void onSuccess(RedPackResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.getList(), page, 10);
                    }

                    @Override
                    public void onFail(String message, RedPackResponse response) {
                        page = HCUtils.refreshFail(recyclerView, page, getContext(), message);
                    }
                }).start(new RedPackResponse());


    }

    public void setType(int type) {
        this.type = type;
        if (adapter != null)
            adapter.setType(type);
    }




}
